package st.business.tepth.business.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.CommonBean;
import st.business.tepth.business.bean.FactoryInfo;
import st.business.tepth.business.event.CheckVersionEvent;
import st.business.tepth.business.event.ReloadVisitorDataEvent;
import st.business.tepth.business.visitor.DialogBindFactory;
import st.business.tepth.config.Url;
import st.shadow.library.core.base.fragment.BaseFragment;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.core.storage.ShadowPreference;
import st.shadow.library.core.util.utilcode.util.AppUtils;
import st.shadow.library.core.util.utilcode.util.DeviceUtils;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;

/**
 * @author zhangyu
 * @date 2019/7/16
 */
public class SettingDelegate extends BaseFragment {
    public static SettingDelegate newInstance() {
        return new SettingDelegate();
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.tv_device_no)
    TextView mTvDeviceNo;
    @BindView(R2.id.tv_bind_factory)
    TextView mTvBindFactory;
    @BindView(R2.id.tv_version)
    TextView mTvVersion;
    private Disposable mDisposable;
    private DialogLoader mDialogLoader;

    @Override
    public Object setLayout() {
        return R.layout.delegate_setting;
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("设置");
        String factoryName = ShadowPreference.getCustomAppProfile("factoryName");
        mTvDeviceNo.setText(DeviceUtils.getAndroidID());
        mTvBindFactory.setText(factoryName);
        mTvVersion.setText(AppUtils.getAppVersionName());
        mDialogLoader = new DialogLoader(_mActivity);
    }

    @OnClick(R2.id.tv_check_update)
    public void onCheckUpdate() {
        EventBus.getDefault().post(new CheckVersionEvent());
        //  ToastUtils.showShort("已经是最新版本");
    }

    @OnClick({R2.id.tv_bind_factory, R2.id.v_bind_factory})
    public void onBind() {
        new DialogBindFactory(_mActivity, null,
                new DialogBindFactory.OnConfirmListener() {

                    @Override
                    public void onChoose(final FactoryInfo.Factory factory,
                                         final CommonBean commonBean) {
                        if (factory != null && commonBean != null) {
                            mDisposable = RxRestClient.builder()
                                    .url(Url.SetDeviceBindInfo)
                                    .params("workbenchNo", DeviceUtils.getAndroidID())
                                    .params("organizationId", factory.getId())
                                    .params("orgGroupId", commonBean.getId())
                                    .success(new ISuccess() {
                                        @Override
                                        public void onSuccess(String response) {
                                            String name = factory.getName() + "-" + commonBean
                                                    .getName();
                                            ShadowPreference.addCustomAppProfile("factoryName",
                                                    name);
                                            mTvBindFactory.setText(name);
                                            EventBus.getDefault().post(new ReloadVisitorDataEvent());
                                        }
                                    })
                                    .error(new IError() {
                                        @Override
                                        public void onError(int code, String msg) {
                                            if (mDialogLoader != null) {
                                                mDialogLoader.showFailed(msg, msg, new IDialogClickListener() {
                                                    @Override
                                                    public void onClick() {
                                                        mDialogLoader.clearDialog();
                                                    }
                                                });
                                            }
                                        }
                                    })
                                    .build()
                                    .post();
                        }
                    }
                }).measureSize().show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }
}
